package com.lyl.syt.service.impl;
/**
 * @author Mr.Luo
 * @date 2022-05-26 17:36
 */

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyl.syt.dao.RefundDao;
import com.lyl.syt.enums.RefundStatusEnum;
import com.lyl.syt.model.order.PaymentInfo;
import com.lyl.syt.model.order.RefundInfo;
import com.lyl.syt.service.RefundService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author Mr.Luo
 * @date 2022-05-26 17:36
 * @desc
 */
@Service
public class RefundServiceImpl extends ServiceImpl<RefundDao, RefundInfo> implements RefundService {
    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {
        RefundInfo refundInfo = lambdaQuery().eq(RefundInfo::getOrderId, paymentInfo.getOrderId())
                .eq(RefundInfo::getPaymentType, paymentInfo.getPaymentType())
                .one();
        if (refundInfo != null) {
            return refundInfo;
        }

        // 保存交易记录
        refundInfo = new RefundInfo();
        refundInfo.setCreateTime(new Date());
        refundInfo.setOrderId(paymentInfo.getOrderId());
        refundInfo.setPaymentType(paymentInfo.getPaymentType());
        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());
        refundInfo.setSubject(paymentInfo.getSubject());
        refundInfo.setTotalAmount(paymentInfo.getTotalAmount());
        save(refundInfo);
        return refundInfo;
    }
}
